<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $table = 'user';

    protected $primaryKey = 'user_id';

    public $timestamps = false;

    protected $hidden = ['user_password', 'user_login_rnd'];
	
	protected $appends = ['user_status_format', 'user_last_login_time_format', 'user_operation', 'user_checkbox', 'user_permission'];
	
	public function getUserStatusFormatAttribute()
    {
        $result = $this->user_status ? '正常' : '禁用';
		return $result;
    }
	
	public function getUserCheckboxAttribute()
    {
        $html = $this->user_id > 1 ? '<label><input type="checkbox" name="checkboxList" value="'.$this->user_id.'" class="input-checkbox"><i>✓</i></label>':'-';
        return $html;
    }
	
	public function getUserLastLoginTimeFormatAttribute()
    {
        if($this->user_last_login_time){
			$result = date("Y-m-d H:i:s", $this->user_last_login_time);
		}else{
			$result = '-';
		}
		return $result;
    }
	
	public function getUserOperationAttribute()
    {
        $html  = "<i class='fa fa-edit' onclick=layOpenView('/admin/user/editview/{$this->user_id}','90%','90%','管理员信息编辑')></i>";
		if($this->user_id > 1){
            if ($this->user_status) {
                $html .= "<i class='fa fa-level-down ml-15' onclick=layChangeStatus('/admin/user/status/{$this->user_id}/0','禁用')></i>";
            } else {
                $html .= "<i class='fa fa-level-up ml-15' onclick=layChangeStatus('/admin/user/status/{$this->user_id}/1','启用')></i>";
            }
            $html .= "<i class='fa fa-trash-o ml-15' onclick=layListDel(this,'/admin/user/destroy','{$this->user_id}','tr')></i>";
        }
        return $html;
    }
	
	public function getUserPermissionAttribute(){
		//获取用户组（注：添加用户组时，字符串前后拼接分隔符，方便匹配查询）
		$group = explode(',',substr($this->user_group,1,-1));
		$permission_arr = UserGroup::whereIn('usergroup_id',$group)->pluck('usergroup_permission');
		$permission = implode('::::::',$permission_arr->toArray());
		return $permission;
	}
}
